<?php
/**
 * Created by PhpStorm.
 * User: 我想有个家
 * Date: 2019/4/23
 * Time: 0:15
 */

namespace App\Controller\admin\admin;


use App\Controller\Base;
use App\Model\AuRule;
use App\Lib\Tree;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * Class Rule
 * @Controller(prefix="a/rule1")
 * @Middleware(\App\Middleware\AdminMiddleware::class)
 */
class RuleController extends Base {

    /**
     * 规则列表
     * @return array
     * @RequestMapping(path="rule",methods="get")
     */
    public function getRule() {
        $p = (int) $this->get('p');
        $lists = AuRule::query()
            ->orderBy('sequence', 'desc')
            ->orderBy('id')
            ->where('parent_id', $p)
            ->select('id', 'parent_id', 'type', 'name', 'path', 'ext', 'sequence')
            ->get()->toArray();
        foreach ($lists as &$list) {
            if ($list['type'] == 1) {
                $list['children'] = [];
            }
        }
        return success(['lists' => $lists]);
    }

    /**
     * 规则列表
     * @return array
     * @RequestMapping(path="menu",methods="get")
     */
    public function getLists() {
        $lists = AuRule::query()
            ->orderBy('sequence', 'desc')
            ->orderBy('id')
            ->select('id', 'parent_id', 'type', 'name', 'path', 'ext', 'sequence')
            ->get()->toArray();
        foreach ($lists as &$list) {
            if ($list['type'] == 1) {
                $list['_showChildren'] = true;
            }
        }
        if ($lists) {
            $lists = Tree::mTree($lists);
        }
        return success(['lists' => $lists]);
    }

    /**
     * 添加/编辑菜单
     * @return array
     * @RequestMapping(path="menu",methods="post")
     */
    public function postMenu() {
        return success();
        $param = [
            'name' => $this->body('name'),
            'path' => $this->body('path'),
            'ext' => $this->body('ext'),
            'parent' => $this->body('parent'),
            'id' => $this->body('id'),
        ];
        if (empty($param['name']) || empty($param['path'])) {
            return error('参数错误');
        }
        // 查询父级
        if ($param['parent']) {
            $parentID = AuRule::query()
                ->where('id', $param['parent'])
                ->where('type', 1)
                ->value('level');
            if ($parentID === null) {
                return error('父级不存在');
            }
            if ($parentID > 0) {
                return error('菜单只能有两级');
            }
        }
        $data = [
            'name' => $param['name'],
            'parent_id' => $param['parent'],
            'path' => $param['path'],
            'ext' => $param['ext'],
        ];
        if ($param['id']) {
            $r = AuRule::query()->where('id', $param['id'])->update($data);
        } else {
            $r = AuRule::query()->insert($data);
        }
        return $r ? success() : error();
    }

    /**
     * 添加/编辑操作
     * @return array
     * @RequestMapping(path="act",methods="post")
     */
    public function postAct() {
        $name = $this->body('name');
        $path = $this->body('path');
        $ext = $this->body('ext');
        $parent = $this->body('parent');
        $id = $this->body('id');
        if (empty($name) || empty($path)) {
            return error('参数错误');
        }
        // 查询父级
        if ($parent) {
            $parentID = AuRule::query()
                ->where('id', $parent)
                ->where('type', 1)
                ->value('parent_id');
            if ($parentID === null) {
                return error('父级不存在');
            }
            if ($parentID == 0) {
                return error('操作只能添加在二级菜单下');
            }
        }
        $data = [
            'name' => $name,
            'type' => 2,
            'parent_id' => $parent,
            'path' => $path,
            'ext' => $ext,
        ];
        if ($id) {
            $r = AuRule::query()
                ->where('id', $id)
                ->where('type', 2)
                ->update($data);
        } else {
            $r = AuRule::query()->insert($data);
        }
        return $r ? success() : error();
    }

    /**
     * 删除权限
     * @RequestMapping(path="del",methods="post")
     */
    public function postDel() {
        $id = $this->body('id');
        $count = AuRule::query()
            ->where('parent_id', $id)
            ->count();
        if ($count > 0) {
            return error('请先删除子级');
        }
        $r = AuRule::query()->where('id', $id)->delete();
        return $r ? success() : error();
    }

    /**
     * 更新排序
     * @return array
     * @RequestMapping(path="sequence",methods="post")
     */
    public function postSequence() {
        $sort = $this->body('sort');
        $id = $this->body('id');
        $r = AuRule::query()
            ->where('id', $id)
            ->update(['sequence' => $sort]);
        return $r ? success() : error();
    }
}
